package yf.mall.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.dao.AccountRecordDao;
import yf.mall.dao.MallOrderDao;
import yf.mall.entity.AccountRecordEntity;
import yf.mall.enums.*;
import yf.mall.service.MyService;
import yf.mall.vo.AccountMyProfitVO;
import yf.mall.vo.MallOrderVo;
import yf.mall.vo.OrderMyInfo;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author xiaolongtongxie
 * @version 1.0
 * @date 2020/6/11 18:17
 */
@Service
public class MyServiceImpl implements MyService {

    @Autowired
    private MallOrderDao mallOrderDao;
    @Autowired
    private AccountRecordDao accountRecordDao;

    /**
     * Long
     * 待付款
     * @param userId
     * @param orderStatus
     * @param payStatus
     * @param limit
     * @param page
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MallOrderVo> myOrderList(Long userId, Integer orderStatus, Integer payStatus, int limit, int page) {
        PageHelper.startPage(page, limit);
        List<MallOrderVo> orderVos = this.mallOrderDao.queryByUserIdAndPayStatus(userId, orderStatus,payStatus);
        return orderVos;
    }
    @Override
    public OrderMyInfo findOrderStatus(Long userId) {
        OrderMyInfo info = new OrderMyInfo();
        Integer wait = this.mallOrderDao.total(userId, MallOrderOrderStatusEnum.NEW.getIndex(), MallOrderPayStatusEnum.NOT_PAID.getIndex());
        if (wait!=null){
            info.setWaitOrderNum(wait);
        }
        Integer delivered = this.mallOrderDao.total(userId, MallOrderOrderStatusEnum.DELIVERED.getIndex(), -1);
        if (delivered!=null) {
            info.setDeliveredOrderNum(delivered);
        }
        return info;
    }

    /** que
     *  我的利润
     * @param userId
     * @return
     */
    @Override
    public AccountMyProfitVO findByUserIdAndBus(Long userId) {

        //奖励
        List<AccountRecordEntity> accountReward = accountRecordDao.findByToUserIdAndBus(userId, AccountRecordBusEnum.SHARE_REWARD.getIndex());
        //追回
        List<AccountRecordEntity> accountBack = accountRecordDao.findByFromUserIdAndBus(userId, AccountRecordBusEnum.BACK.getIndex());

        BigDecimal agentReward = agentReward(userId); //代理有利实收款
        BigDecimal retailsTotal = retailsReward(userId, accountReward, accountBack); //分享有利实收款
        BigDecimal voucherTotal = voucherReward(userId, accountReward, accountBack); // 换购有利实收款
        AccountMyProfitVO myProfit = new AccountMyProfitVO(agentReward,retailsTotal,voucherTotal);

        return myProfit;
    }

    //代理有利
    private BigDecimal agentReward(Long userId){
        //待收款
        List<AccountRecordEntity> accountReward = accountRecordDao.findByToUserIdAndBus(userId, AccountRecordBusEnum.PAYMENT.getIndex());
        //自动进货款
        List<AccountRecordEntity> accountBack = accountRecordDao.findByFromUserIdAndBus(userId, AccountRecordBusEnum.AUTO_PAY.getIndex());

        //总收款
        BigDecimal agentReward = getAgentAmount(accountReward);
        //自动进货款
        BigDecimal agentBack = getAgentAmount(accountBack);

        return agentReward.subtract(agentBack);
    }

    //获取代理有利累积收款和自动进货款
    private BigDecimal getAgentAmount(List<AccountRecordEntity> accountReward) {
        BigDecimal agentReward = new BigDecimal(0);
        if (null != accountReward && accountReward.size() > 0){
            for (AccountRecordEntity recordEntity : accountReward) {
                if (null != recordEntity.getType() && recordEntity.getType() == AccountRecordTypeEnum.CASH.getIndex() && recordEntity.getStatus() == AccountRecordStatusEnum.DONE.getIndex()){
                    if (null != recordEntity.getAmount()){
                        agentReward = agentReward.add(recordEntity.getAmount());
                    }
                }
            }
        }
        return agentReward;
    }

    //换购有利
    private BigDecimal voucherReward(Long userId,List<AccountRecordEntity> accountReward,List<AccountRecordEntity> accountBack) {
        //奖励
        BigDecimal voucherReward = new BigDecimal(0);
        //追回
        BigDecimal voucherBack = new BigDecimal(0);
        //奖励的
        if (null != accountReward && accountReward.size() > 0){
            for (AccountRecordEntity recordEntity : accountReward) {
                if (null != recordEntity.getType() && recordEntity.getType() == AccountRecordTypeEnum.VOUCHER.getIndex() && recordEntity.getStatus() == AccountRecordStatusEnum.DONE.getIndex()){
                    if (null != recordEntity.getAmount()){
                        voucherReward = voucherReward.add(recordEntity.getAmount());
                    }
                }
            }
        }
        //退回的
        if (null != accountBack && accountBack.size() > 0){
            for (AccountRecordEntity recordEntity : accountBack) {
                if (null != recordEntity.getType() && recordEntity.getType() == AccountRecordTypeEnum.VOUCHER.getIndex() && recordEntity.getStatus() == AccountRecordStatusEnum.DONE.getIndex()){
                    if (null != recordEntity.getAmount()){
                        if (recordEntity.getToUserId() == userId){
                            continue;
                        }
                        voucherBack = voucherBack.add(recordEntity.getAmount());
                    }
                }
            }
        }
        return voucherReward.subtract(voucherBack);
    }

    //分享有利
    private BigDecimal retailsReward(Long userId,List<AccountRecordEntity> accountReward,List<AccountRecordEntity> accountBack) {
        //奖励
        BigDecimal retailsReward = new BigDecimal(0);
        //追回
        BigDecimal retailsBack = new BigDecimal(0);
        //奖励的
        if (null != accountReward && accountReward.size() > 0){
            for (AccountRecordEntity recordEntity : accountReward) {
                if (null != recordEntity.getType()) {
                    //分享有利累积包含现金券、优惠券、购物券
                    if (recordEntity.getType() == AccountRecordTypeEnum.CASH.getIndex() ||
                            recordEntity.getType() == AccountRecordTypeEnum.COUPON.getIndex() ||
                            recordEntity.getType() == AccountRecordTypeEnum.VOUCHER.getIndex()) {
                        if (null != recordEntity.getAmount() && recordEntity.getStatus() == AccountRecordStatusEnum.DONE.getIndex()){
                            retailsReward = retailsReward.add(recordEntity.getAmount());
                        }
                    }
                }
            }
        }
        //退回的
        if (null != accountBack && accountBack.size() > 0){
            for (AccountRecordEntity recordEntity : accountBack) {
                if (null != recordEntity.getType()) {
                    //分享有利累积包含现金券、优惠券、购物券
                    if (recordEntity.getType() == AccountRecordTypeEnum.CASH.getIndex() ||
                            recordEntity.getType() == AccountRecordTypeEnum.COUPON.getIndex() ||
                            recordEntity.getType() == AccountRecordTypeEnum.VOUCHER.getIndex()) {
                        if (null != recordEntity.getAmount() && recordEntity.getStatus() == AccountRecordStatusEnum.DONE.getIndex()){
                            if (recordEntity.getToUserId() == userId){
                                continue;
                            }
                            retailsBack = retailsBack.add(recordEntity.getAmount());
                        }
                    }
                }
            }
        }
        return retailsReward.subtract(retailsBack);
    }

}
